#include <QtTest>
#include "../tests_common.h"

#include "bitwin/roles.h"
#include "bitwin/bit_range.h"
#include "bitwin/rows.h"
#include "bitwin/columns.h"


using namespace BitWin;


Q_DECLARE_METATYPE(BitWin::PGenericBitwise)


class TestDisplayColumn: public QObject
{
    Q_OBJECT

private slots:

    void testWithWordRow_data();
    void testWithWordRow();

    void testWithFieldRow_data();
    void testWithFieldRow();
};


void TestDisplayColumn::testWithWordRow_data()
{
    QTest::addColumn<QString>("title");
    QTest::addColumn<int>("role");
    QTest::addColumn<PGenericBitwise>("indata");
    QTest::addColumn<QString>("outdata_expected");
    
    QTest::newRow("hex role of string")
        << QString("column 1") << HEX_DATA_ROLE
        << STRING_BITWISE("abcd")
        << QString("HEX(abcd)")
    ;
    QTest::newRow("hex role of quint32")
        << QString("column #2") << HEX_DATA_ROLE
        << GenericBitwise::fromIntegral<quint32>(0xC0D4F00D)
        << QString("0xc0d4f00d")
    ;
    QTest::newRow("dec role of string")
        << QString("column N3") << DEC_DATA_ROLE
        << STRING_BITWISE("abcd")
        << QString("DEC(abcd)")
    ;
    QTest::newRow("dec role of quint16")
        << QString("column.4") << DEC_DATA_ROLE
        << GenericBitwise::fromIntegral<quint16>(0xbe11)
        << QString("48657")
    ;
}

void TestDisplayColumn::testWithWordRow()
{
    QFETCH(QString, title);
    QFETCH(int, role);
    
    DisplayColumn column(title, role);
    QCOMPARE(column.title(), title);
    
    WordRow row("row", 0);
    
    QFETCH(PGenericBitwise, indata);
    
    QVERIFY(!column.data(BitwiseDataRole, &row, indata).isValid());
    QVERIFY(!column.data(role, &row, indata).isValid());
    
    QVariant data(column.data(Qt::DisplayRole, &row, indata));
    QVERIFY(data.isValid());
    QVERIFY(data.canConvert<QString>());
    QString outdata(data.value<QString>());
    
    QFETCH(QString, outdata_expected);
    QCOMPARE(outdata, outdata_expected);
}

void TestDisplayColumn::testWithFieldRow_data()
{
    QTest::addColumn<int>("role");
    QTest::addColumn<BitRange>("range");
    
    QTest::newRow("Hex column, valid range")
        << HEX_DATA_ROLE << BitRange(1, 0)
    ;
    QTest::newRow("Dec column, valid range")
        << DEC_DATA_ROLE << BitRange(1, 0)
    ;
    
    QTest::newRow("Hex column, null range")
        << HEX_DATA_ROLE << BitRange()
    ;
    QTest::newRow("Dec column, null range")
        << DEC_DATA_ROLE << BitRange()
    ;
}

void TestDisplayColumn::testWithFieldRow()
{
    QFETCH(int, role);
    DisplayColumn col("Column", role);
    
    QFETCH(BitRange, range);
    FieldRow row("row", range, 0);
    
    PGenericBitwise indata(STRING_BITWISE("foo bar baz"));
    
    QVERIFY(!col.data(BitwiseDataRole, &row, indata).isValid());
    QVERIFY(!col.data(role, &row, indata).isValid());
    QVERIFY(!col.data(Qt::DisplayRole, &row, indata).isValid());
}


QTEST_MAIN(TestDisplayColumn)


#include "main.moc"
